<?php 
  //test query http://localhost/onmotion/listPointsGeoRss.php?userLogin=SergioEstevao&userTid=111111
  
  include_once('session.php');
  include_once('db.php');
  include_once('constants.php');
  header("Content-Type: application/rss+xml; charset=utf-8");
  //header("Content-Type: application/vnd.google-earth.kml+xml; charset=utf-8");
  //header('Content-Disposition: attachment; filename="findme.kml"');
       
  $doc = new DomDocument('1.0', 'UTF-8');
  $doc->formatOutput = true;
  $kmlNs = $doc->createElement('rss');
  $kmlNs->setAttribute('xmlns:georss', 'http://www.georss.org/georss');
  $kmlNs->setAttribute('version', '2.0');
  $doc->appendChild($kmlNs);
   
  $document = $doc->createElement('channel');
      
  $name = $doc->createElement('title');
  $nameValue = $doc->createTextNode($product_name);
  $name->appendChild($nameValue);
  $document->appendChild($name);
    
  $description = $doc->createElement('description');
  $descriptionValue = $doc->createTextNode($product_name.' provides you a Geographic Blog where you can find friends and events around you.');
  $description->appendChild($descriptionValue);  
  $document->appendChild($description);
  
  $date = $doc->createElement('pubDate', gmdate('r')); 
  $document->appendChild($date);
  
  $link = $doc->createElement('link','http://'.$_SERVER["HTTP_HOST"]);
  //.'/'.$_SERVER["SCRIPT_NAME"].'?userLogin='.$sessionUserLogin.'&userTid='.$sessionUserTid);  
  $document->appendChild($link);
            
  if ( IsSessionValid())
  {          
    $conn = &ADONewConnection($dbType);
    $conn->PConnect($dbServer, $dbUser, $dbPassword, $dbName);
    //$sqlfn = $conn->SQLDate('H m s Y-m-d H:m:s','time');   
    $query = "select lat, lng, altitude, name, comment, time, id_point from point where type <> 3 and id_user=$sessionUserId order by time desc";
    //execute query
    //$conn->debug = true;      
    $rs = $conn->Execute($query);
              
    // create Point Placemark
    foreach ($rs as $row) 
    {
      $placeMark = $doc->createElement('item');
      $placeMark->appendChild($doc->createElement("guid",'http://'.$_SERVER["HTTP_HOST"].'/showPost.php?id='.$row[6]));
      $name = $doc->createElement('title');
      $nameValue = $doc->createTextNode($row[3]);
      $name->appendChild($nameValue);
      $placeMark->appendChild($name);
      
      $description = $doc->createElement('description');
      $descriptionValue = $doc->createTextNode($row[4]);
      $description->appendChild($descriptionValue);
      $placeMark->appendChild($description);
      
      $date = $doc->createElement('pubDate');
      $dateValue = $doc->createTextNode(date('r',strtotime($row[5])));
      $date->appendChild($dateValue);
      $placeMark->appendChild($date);
            
      $point = $doc->createElement('georss:point');
      $coordinates = $doc->createElement('coordinates');
      $pointValue = $doc->createTextNode($row[0].' '.$row[1]);
      //$coordinates->appendChild($pointValue);
      //$point->appendChild($coordinates);
      $point->appendChild($pointValue);                
      $placeMark->appendChild($point);
                 
      // select resources
      $image = $doc->createElement('image');
      $imageValue = $doc->createTextNode("0");
      $image->appendChild($imageValue);
      $queryResource = "select id_image from image where id_point=$row[6]";
      $rsResource = $conn->Execute($queryResource);
      foreach($rsResource as $rowResource) {
          $image->replaceChild($doc->createTextNode($rowResource[0]),$imageValue);
      }
      //$placeMark->appendChild($image);
      
      $document->appendChild($placeMark);                 
    }
    $rs->close();
    $conn->close();
  }      
   
  $kmlNs->appendChild($document);        
     
  // save to xml file
  $xml_string = $doc->saveXML();
  // output in UTF-8
  echo $xml_string;
  // echo iconv("ISO-8859-1", "UTF-8",$xml_string);
?>
